﻿using Dapper;
using HR.Config;
using HR.IService;
using HR.Models;
using System.Data.SqlClient;

namespace HR.Service
{
    public class Config_majorKindService : IConfig_majorKindService
    {
        private readonly HRDBContext db;
        private readonly IConfiguration icon;
        //数据库连接字符串
        private readonly string? connstr;
        public Config_majorKindService(HRDBContext db, IConfiguration icon)
        {
            this.db = db;
            this.icon = icon;
            this.connstr = this.icon["ConnectionStrings"];
        }
        
        /// <summary>
        /// 职位分类刷新不带分页
        /// </summary>
        /// <returns></returns>
        public Task<List<config_major_kind>> CMKOnPageFill()
        {

            return Task.Run(() =>
            {
                return db.config_major_kinds.ToList();
            });
        }
        /// <summary>
        /// 职位分类刷新带分页
        /// </summary>
        /// <returns></returns>
        public Task<Paging<config_major_kind>> CMajorKindFill(int pgsize, int pgcurrent)
        {
            return Task.Run(() =>
            {
                List<config_major_kind> list = db.config_major_kinds.ToList();
                List<config_major_kind> cmks = list.Skip((pgcurrent - 1) * pgsize).Take(pgsize).ToList();
                Paging<config_major_kind> pg = new Paging<config_major_kind>()
                {
                    rows = list.Count,
                    tabs = cmks
                };
                return pg;
            });
        }
        /// <summary>
        /// 新增职位分类
        /// </summary>
        /// <param name="cmk"></param>
        /// <returns></returns>
        public Task<int> CMajorKindAdd(config_major_kind cmk)
        {
            return Task.Run(() =>
            {
                config_major_kind? cm = db.config_major_kinds.OrderByDescending(cmk => cmk.major_kind_id).FirstOrDefault();
                cmk.major_kind_id= (Int32.Parse(cm.major_kind_id)+1).ToString();
                db.config_major_kinds.Add(cmk);
                return db.SaveChanges();
            });
        }
        /// <summary>
        /// 删除职位分类
        /// </summary>
        /// <param name="mfk_id"></param>
        /// <returns></returns>
        public Task<int> CMajorKIndDelete(short mfk_id)
        {
            return Task.Run(() =>
            {
                config_major_kind? cmk = db.config_major_kinds.Find(mfk_id);
                db.config_major_kinds.Remove(cmk);
                return db.SaveChanges();
            });
        }
        /// <summary>
        /// 修改职位分类
        /// </summary>
        /// <param name="cmk"></param>
        /// <returns></returns>
        public Task<int> CMajorKindUpdate(config_major_kind cmk)
        {
            return Task.Run(() =>
            {
                config_major_kind? cmkind = db.config_major_kinds.Find(cmk.mfk_id);
#pragma warning disable CS8602 // 解引用可能出现空引用。
                cmkind.major_kind_id = cmk.major_kind_id;
#pragma warning restore CS8602 // 解引用可能出现空引用。
                cmkind.major_kind_name = cmk.major_kind_name;
                return db.SaveChanges();
            });
        }
        /// <summary>
        /// 职位类型中囊括多条职位名称：用于联级选择器
        /// </summary>
        /// <returns></returns>
        public Task<List<CMajorKind>> CMKCascader()
        {
            return Task.Run(() =>
            {
                using (SqlConnection sqlcon = new SqlConnection(connstr))
                {
                    string sql = "select [major_kind_id] as id,[major_kind_name] as name from config_major_kind";
                    List<CMajorKind> list = sqlcon.Query<CMajorKind>(sql).ToList();
                    foreach (CMajorKind c in list)
                    {
                        string sql1 = $@"select [major_id] as id,[major_name] as name from config_major where [major_kind_id]={c.Id}";
                        List<CMajor> children = sqlcon.Query<CMajor>(sql1).ToList();
                        c.children = children;
                    }
                    return list;
                }
            });
        }

    }
}
